export default {
    /**
     * 防抖函数
     * @param {function} fn
     * @param {time} delay 延迟时间默认500ms
     * @param {immediate} 立即执行
     */
    debounce(fn, delay = 500, immediate = true) {
        //默认300毫秒
        let timeout, result;
        return function () {
            let context = this;
            let args = arguments;
            if (timeout) clearTimeout(timeout);
            if (immediate) {
                let callNow = !timeout;
                timeout = setTimeout(function () {
                    timeout = null;
                }, delay);
                if (callNow) result = fn.apply(context, args);
            } else {
                timeout = setTimeout(function () {
                    fn.apply(context, args);
                }, delay);
            }
            return result;
        };
    },
    /** 
     * 节流
     * @param { function } func 函数
     * @param { number } wait 延迟执行毫秒数
     * @param { number } type 1 表时间戳版，2 表定时器版
     */
    throttle(func, wait, type = 1) {
        let previous, timeout;
        if (type === 1) {
            previous = 0;
        } else if (type === 2) {
            timeout = null;
        }
        return function () {
            let context = this;
            if (type === 1) {
                let now = Date.now();
                if (now - previous > wait) {
                    func.apply(context, args);
                    previous = now;
                }
            } else if (type === 2) {
                if (!timeout) {
                    timeout = setTimeout(() => {
                        timeout = null;
                        func.apply(context, args);
                    }, wait);
                }
            }
        };
    },

    /**
     * 整合图表数据结构
     */
    changeChartsDatas(data) {},
    /** 
     * 深度遍历
     */
    deepTraversing(node, nodeList = [], key = 'childList') {
        if (node !== null) {
            for (let i = 0; i < node.length; i++) {
                nodeList.push(node);
                let children = node[key];
                if (children && children.length > 0) {
                    this.format(children, nodeList);
                }
            }
        }
        return nodeList;
    },
    /** 
     * 获取url参数
     */
    getUrlParam(name, origin = null) {
        let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        let r = null;
        if (origin == null) {
            r = window.location.search.substr(1).match(reg);
        } else {
            r = origin.substr(1).match(reg);
        }
        if (r != null) return decodeURIComponent(r[2]);
        return null;
    },
    /** 
     * 开启全屏
     */
    launchFullscreen(element) {
        if (element.requestFullscreen) {
            element.requestFullscreen();
        } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen();
        } else if (element.msRequestFullscreen) {
            element.msRequestFullscreen();
        } else if (element.webkitRequestFullscreen) {
            element.webkitRequestFullScreen();
        }
    },
    /** 
     * 关闭全屏
     */
    exitFullscreen() {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitExitFullscreen) {
            document.webkitExitFullscreen();
        }
    },
    /** 
     *localStorage 存储 
     */
    localStorageSet(key, value) {
        if (typeof (value) === 'object') value = JSON.stringify(value);
        localStorage.setItem(key, value);
    },
    /** 
     * locationStorage 删除
     */
    localStorageRemove(key) {
        localStorage.removeItem(key);
    },
    /** 
     * sessionStorage 存储
     */
    sessionStorageGet(key) {
        sessionStorage.getItem(key);
    },
    /** 
     * sessionStorage 删除
     */
    sessionStorageRemove(key) {
        sessionStorage.removeItem(key);
    },
    /** 
     * 截取字符串并加省略号
     */
    subText(str, length) {
        if (str.length === 0) {
            return '';
        }
        if (str.length > length) {
            return str.substr(0, length) + '...';
        } else {
            return str;
        }
    },
    getOsType() {
        let u = navigator.userAgent,
            app = navigator.appVersion;
        let isAndroid = u.includes('Android') || u.includes('Linux');
        let isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
        if (isIos) {
            return 0;
        }
        if (isAndroid) {
            return 1;
        }
        return 2;
    },
    escapeHTML(str) {
        str.replace(
            /[&<>'"]/g,
            tag => ({
                '&': '&amp;',
                '<': '&lt;',
                '>': '&gt;',
                "'": '&#39;',
                '"': '&quot;'
            } [tag] || tag)
        );
    },
    drag(context, offsetLeft, offsetTop) {
        document.onmousedown = function (en) {
            let ev = ev || event;
            let disX = en.clientX - offsetLeft;
            let disY = en.clientY - offsetTop;
            document.onmousemove = function (en) {
                let ev = ev || event;
                if (
                    en.clientY > 0 &&
                    en.clientY < context.clientHeight &&
                    en.clientX > 0 &&
                    en.clientX < context.clientWidth
                ) {
                    context.top = en.clientY - disY;
                    context.left = en.clientX - disX;
                }
            };
            document.onmouseup = function () {
                document.onmousemove = null;
            };
            return false;
        };
    },
};